//
//  JGoAPI.m
//  jungoo
//
//  Created by Sinbad Fly on 8/15/12.
//  Copyright (c) 2012 ABOM. All rights reserved.
//

#import "JGoAPI.h"

#define JGO_API_HANDLE_ERROR(code,msg)\
if (error != nil) {\
if (msg != nil) *error = [JGoAPI errorWithCode:code description:msg];\
else *error = [JGoAPI errorWithCode:code description:@"An exception error"];\
}


// Error Description

#define JGO_ERROR_MSG_NETWORK         (@"Unable to connect to the network please check your network connection and try again")
#define JGO_ERROR_MSG_DATA            (@"Data from the server returns incorrectly!")
#define JGO_ERROR_MSG_UNKNOW          (@"An error occurred unspecified!")


// JGoAPI implementation

@implementation JGoAPI

+ (void)postUploadWithURL:(NSString *)url requestData:(NSDictionary *)requestData postData :(NSData *)aData
{
    NSURL *theURL = [NSURL URLWithString:url];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL
                                                              cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                          timeoutInterval:20.0f];
    [theRequest setHTTPMethod:@"POST"];
    
    // define post boundary...
    NSString *boundary = [[NSProcessInfo processInfo] globallyUniqueString];
    NSString *boundaryString = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [theRequest addValue:boundaryString forHTTPHeaderField:@"Content-Type"];
    
    // define boundary separator...
    NSString *boundarySeparator = [NSString stringWithFormat:@"--%@\r\n", boundary];
    
    //adding the body...
    NSMutableData *postBody = [NSMutableData data];
    
    // adding params...
    for (id key in requestData) {
        NSString *formDataName = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key];
        NSString *formDataValue = [NSString stringWithFormat:@"%@\r\n", [requestData objectForKey:key]];
        [postBody appendData:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[formDataName dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[formDataValue dataUsingEncoding:NSUTF8StringEncoding]];
    }
    // if file is defined, upload it...
    if (aData) {
        //NSArray *split = [filePath componentsSeparatedByString:@"/"];
        //NSString *fileName = (NSString*)[split lastObject];
        //NSData *fileContent = [NSData dataWithContentsOfFile:filePath options:0 error:nil];
        
        [postBody appendData:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"photo\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Type: application/octet-stream\r\n\r\n"
                              dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:aData];
    }
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r \n",boundary]
                          dataUsingEncoding:NSUTF8StringEncoding]];
    [theRequest setHTTPBody:postBody];  
    
    NSURLResponse *theResponse = NULL;
    NSError *theError = NULL;
    NSData *reqResults = [NSURLConnection sendSynchronousRequest:theRequest
                                               returningResponse:&theResponse
                                                           error:&theError];
    if (!reqResults) {
        NSLog(@"Connection error for URL: %@", [url description]);
        UIAlertView *alert = [UIAlertView alloc];
        [[alert initWithTitle:@"Error in connection"
                      message:[theError localizedDescription]
                     delegate:self
            cancelButtonTitle:nil
            otherButtonTitles:@"Quit", nil] autorelease];
        [alert show];
    }
}

+ (NSError*)errorWithCode:(NSInteger)code description:(NSString*)description
{
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:description 
                                                         forKey:NSLocalizedDescriptionKey];
    return [NSError errorWithDomain:JGO_ERROR_DOMAIN 
                               code:code userInfo:userInfo];
}

+ (NSArray*)getUniversity:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/university.php",JGO_HOST];
    NSURL *url = [NSURL URLWithString:urlString];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getUniversity.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSArray *universities = [responseString JSONValue];
            
            if (universities == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                
                if ([universities isKindOfClass:[NSArray class]]) {
                    
                    if ([universities count] == 0) {
                        return nil;
                    }
                    else {
                        return universities;
                    }
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                         JGO_ERROR_MSG_DATA);
                    
                    return nil;
                }
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;                 
}

+ (NSArray*)getModules:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/modules.php",JGO_HOST];
    NSURL *url = [NSURL URLWithString:urlString];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getModules.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSArray *universities = [responseString JSONValue];
            
            if (universities == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                
                if ([universities isKindOfClass:[NSArray class]]) {
                    
                    if ([universities count] == 0) {
                        return nil;
                    }
                    else {
                        return universities;
                    }
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                         JGO_ERROR_MSG_DATA);
                    
                    return nil;
                }
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;               
}

+ (NSArray*)getCourses:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/courses.php",JGO_HOST];
    NSURL *url = [NSURL URLWithString:urlString];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getCourses.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSArray *courses = [responseString JSONValue];
            
            if (courses == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                
                if ([courses isKindOfClass:[NSArray class]]) {
                    
                    if ([courses count] == 0) {
                        return nil;
                    }
                    else {
                        return courses;
                    }
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                         JGO_ERROR_MSG_DATA);
                    
                    return nil;
                }
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;               
}

+ (NSArray*)getColleges:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/colleges.php",JGO_HOST];
    NSURL *url = [NSURL URLWithString:urlString];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getColleges.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSArray *colleges = [responseString JSONValue];
            
            if (colleges == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                
                if ([colleges isKindOfClass:[NSArray class]]) {
                    
                    if ([colleges count] == 0) {
                        return nil;
                    }
                    else {
                        return colleges;
                    }
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                         JGO_ERROR_MSG_DATA);
                    
                    return nil;
                }
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;               
}

+ (BOOL)registerAccountWithName:(NSString*)name surname:(NSString*)surname 
                          email:(NSString*)email password:(NSString*)password 
                     university:(NSString*)university lat:(NSString*)lat 
                            lon:(NSString*)lon error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/register.php",JGO_HOST];
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSString *bodyString = [NSString stringWithFormat:@"name=%@&surname=%@&email=%@&password=%@&university=%@&lat=%@&lon=%@",
                            name,surname,email,password,university,lat,lon];
    
    LOG(@"%@", bodyString);
    
    NSData *postBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    [theRequest setHTTPMethod:@"POST"];    
    [theRequest addValue:[NSString stringWithFormat:@"%d",[postBody length]] forHTTPHeaderField:@"Content-Length"];     
    [theRequest setHTTPBody:postBody];     
    [theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/register.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictResponse = [responseString JSONValue];
            
            if (dictResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                if ([[dictResponse objectForKey:@"returnCode"] isEqualToString:@"0"]) {
                    return YES;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictResponse objectForKey:@"msg"]);
                    return NO;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;                      
}

+ (BOOL)updateProfileWithUserId:(NSString*)userId name:(NSString*)name surname:(NSString*)surname birthDay:(NSString*)birthDay
                         gender:(NSString*)gender email:(NSString*)email
                         status:(NSString*)status college:(NSString*)college
                     university:(NSString*)university course:(NSString*)course
                         u_year:(NSString*)u_year c_year:(NSString*)c_year
                         degree:(NSString*)degree modules:(NSString*)modules
                          thumb:(NSString*)thumb error:(NSError**)error;
{    
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/updateProfile?uid=%@",JGO_HOST,userId];
    NSURL *url = [NSURL URLWithString:urlString];       
    
    NSString *bodyString = [NSString stringWithFormat:@"name=%@&surname=%@&dob=%@&gender=%@&email=%@&status=%@&college=%@&university=%@&course=%@&u_year=%@&c_year=%@&degree=%@&modules=%@&thumb=%@",
                            name,surname,birthDay,gender,email,status,college,university,course,u_year,c_year,degree,modules,thumb];
    
    LOG(@"url : %@ post: %@",urlString, bodyString);
          
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSData *postBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];       
    
    [theRequest setHTTPMethod:@"POST"];    
    [theRequest addValue:[NSString stringWithFormat:@"%d",[postBody length]] forHTTPHeaderField:@"Content-Length"];     
    [theRequest setHTTPBody:postBody];     
    [theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/updateprofile.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictResponse = [responseString JSONValue];
            
            if (dictResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                if ([[dictResponse objectForKey:@"returnCode"] isEqualToString:@"0"]) {
                    return YES;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictResponse objectForKey:@"msg"]);
                    return NO;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;                          
}

+ (NSDictionary*)loginWithEmail:(NSString*)email andPassword:(NSString*)password error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/login",JGO_HOST];
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSString *bodyString = [NSString stringWithFormat:@"email=%@&pass=%@",
                            email,password];
    
    //LOG(@"%@", bodyString);
    
    NSData *postBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    [theRequest setHTTPMethod:@"POST"];    
    [theRequest addValue:[NSString stringWithFormat:@"%d",[postBody length]] forHTTPHeaderField:@"Content-Length"];     
    [theRequest setHTTPBody:postBody];     
    [theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/login.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictResponse = [responseString JSONValue];
            NSLog(@"CurrentUserInfo = %@", dictResponse);
            if (dictResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([[dictResponse objectForKey:@"returnCode"] isEqualToString:@"0"]) {
                    return dictResponse;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictResponse objectForKey:@"msg"]);
                    return nil;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;                     
}

+ (NSDictionary*)loginWithFacebookId:(NSString*)facebookId fb_name:(NSString*)fb_name 
                              fb_dob:(NSString*)fb_dob gender:(NSString*)fb_gender 
                            fb_email:(NSString*)fb_email fb_username:(NSString*)fb_username 
                               error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/fb_login.php",JGO_HOST];
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSString *bodyString = [NSString stringWithFormat:@"name=%@&dob=%@&gender=%@&email=%@&fb_id=%@&fb_username=%@",
                            fb_name,fb_dob,fb_gender,fb_email,facebookId,fb_username];
    
    LOG(@"%@", bodyString);
    
    NSData *postBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    [theRequest setHTTPMethod:@"POST"];    
    [theRequest addValue:[NSString stringWithFormat:@"%d",[postBody length]] forHTTPHeaderField:@"Content-Length"];     
    [theRequest setHTTPBody:postBody];     
    [theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/loginFB.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictResponse = [responseString JSONValue];
            
            if (dictResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([[dictResponse objectForKey:@"returnCode"] isEqualToString:@"0"]) {
                    return dictResponse;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictResponse objectForKey:@"msg"]);
                    return nil;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;                         
}

+ (NSArray*)getPeopleWithUserId:(NSString*)userId searchBy:(NSString*)searchBy 
                          isFav:(BOOL)isFav filter:(NSString*)filter 
                            lat:(NSString*)lat lon:(NSString*)lon error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/people?userId=%@&searchBy=%@&isFav=%@&filter=%@&lat=%@&lon=%@",
                           JGO_HOST,userId,searchBy,(isFav ? @"true":@"false"),filter,lat,lon];    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:[NSString stringWithFormat:@"/JGO/getPeople-%@.txt",searchBy] atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        NSLog(@"%@",responseString);
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                 if ([[dictionaryResponse objectForKey:@"status"] intValue] == 0) {
                    return [dictionaryResponse objectForKey:@"users"];
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictionaryResponse objectForKey:@"msg"]);
                    return nil;
                }      
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;                   
}

+ (UIImage*)downloadAvatar:(NSString*)suffixLink error:(NSError**)error
{
    NSInteger time = (NSInteger)[[NSDate date] timeIntervalSince1970];
    NSString *urlString = [NSString stringWithFormat:@"%@/%@?time=%d",JGO_HOST,suffixLink,time];
    NSURL *url = [NSURL URLWithString:urlString];
    
    LOG(@"%@", urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        if (responseData == nil || ([responseData length] == 0)) {
            JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                 JGO_ERROR_MSG_DATA);
        }
        else {
            UIImage *avatar = [UIImage imageWithData:responseData];            
            return avatar;
        }
    }
    
    return nil;
}

+ (NSDictionary*)addFavoriteWithUserId:(NSString*)userId peopleId:(NSString*)peopleId error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/addFav?uid1=%@&uid2=%@",JGO_HOST,userId,peopleId];
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"URL = %@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/favorites.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            NSLog(@"%@",dictionaryResponse);
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                NSString *status = [dictionaryResponse objectForKey:@"status"];
                if (![status intValue]) {
                    return dictionaryResponse;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictionaryResponse objectForKey:@"msg"]);
                    return nil;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;                    
}

+ (NSArray*)getPhotoWithUserId:(NSString*)userId error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/list_image.php?uid=%@",JGO_HOST,userId];   
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getPhoto.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSArray *arrayPhoto = [responseString JSONValue];
            
            if (arrayPhoto == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([arrayPhoto isKindOfClass:[NSArray class]]) {
                    return arrayPhoto;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return nil;
                }      
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;                       
}

+ (NSDictionary*)getOffersWithUserId:(NSString*)userId lat:(NSString*)lat lon:(NSString*)lon category:(NSString*)cat_id error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/getOffers?uid=%@&lat=%@&lon=%@&cat_id=%@",JGO_HOST,userId,lat,lon,cat_id];
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"URL = %@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getOffers.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            NSLog(@"Result = %@",dictionaryResponse);
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                NSString *status = [dictionaryResponse objectForKey:@"status"];
                if (![status intValue]) {
                    return dictionaryResponse;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictionaryResponse objectForKey:@"msg"]);
                    return nil;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;                    
}

+ (NSDictionary*)getEventsWithUserId:(NSString*)userId lat:(NSString*)lat lon:(NSString*)lon error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/listEvent?uid=%@&lat=%@&lon=%@",JGO_HOST,userId,lat,lon];   
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getEvents.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    
                    if ([[dictionaryResponse objectForKey:@"returnCode"] integerValue] == 0) {
                        return dictionaryResponse;
                    }
                    
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictionaryResponse objectForKey:@"msg"]);
                    
                    return nil;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return nil;
                }      
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;               
}

+ (NSArray*)getPeopleBlockedWithUserId:(NSString*)userId error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/getBlockedUsers?uid=%@",JGO_HOST,userId];   
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getPeopleBlocked.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];           
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    
                    if ([[dictionaryResponse objectForKey:@"returnCode"] integerValue] == 0) {
                        NSArray *blockedPeople = [dictionaryResponse objectForKey:@"data"];
                        return ([blockedPeople isKindOfClass:[NSArray class]] ? blockedPeople : nil);
                    }
                    else {
                        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                             [dictionaryResponse  objectForKey:@"msg"]);
                        return nil;
                    }
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return nil;
                }      
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;                  
}

+ (BOOL)setLocationWithUserId:(NSString*)userId lat:(NSString*)lat lon:(NSString*)lon error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/update_user_location.php?uid=%@&lat=%@&lon=%@",JGO_HOST,userId,lat,lon];
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"URL = %@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/updateLocation.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            NSLog(@"Result = %@",dictionaryResponse);
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                NSString *status = [dictionaryResponse objectForKey:@"returnCode"];
                if (![status intValue]) {
                    return YES;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictionaryResponse objectForKey:@"msg"]);
                    return NO;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;          
}

+ (BOOL)sendFeedbackWithUserId:(NSString*)userId type:(NSString*)type content:(NSString*)content error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/feedback.php",JGO_HOST];
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSString *bodyString = [NSString stringWithFormat:@"uid=%@&type=%@&content=%@",userId,type,content];
    
    LOG(@"%@", bodyString);
    
    NSData *postBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    [theRequest setHTTPMethod:@"POST"];    
    [theRequest addValue:[NSString stringWithFormat:@"%d",[postBody length]] forHTTPHeaderField:@"Content-Length"];     
    [theRequest setHTTPBody:postBody];     
    [theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/sendFeedback.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictResponse = [responseString JSONValue];
            
            if (dictResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                if ([[dictResponse objectForKey:@"returnCode"] isEqualToString:@"0"]) {
                    return YES;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictResponse objectForKey:@"msg"]);
                    return NO;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;                     
} 

+(BOOL)blockPersonWithUserID:(NSString*)userId  blockedUser:(NSString*)blockedUserID error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/block?uid1=%@&uid2=%@",JGO_HOST,userId,blockedUserID];
    NSURL *url = [NSURL URLWithString:urlString];    
    
    LOG(@"%@", urlString);    
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];        
    [theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/blockuser.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictResponse = [responseString JSONValue];
            NSLog(@"%@",dictResponse);
            if (dictResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                NSString *status = [dictResponse objectForKey:@"returnCode"];
                if (![status intValue]) {
                    return YES;
                }  
 
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictResponse objectForKey:@"msg"]);
                    return NO;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;
}


+(BOOL)unblockPersonWithUserID:(NSString*)userId  UnblockedUser:(NSString*)unblockedUserID error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/unblock?uid1=%@&uid2=%@",JGO_HOST,userId,unblockedUserID];
    NSURL *url = [NSURL URLWithString:urlString];
    
    LOG(@"%@", urlString);
       
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];        
    [theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/unblockuser.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictResponse = [responseString JSONValue];
            NSLog(@"%@",dictResponse);
            if (dictResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                NSString *status = [dictResponse objectForKey:@"returnCode"];
                if (![status intValue]) {
                    return YES;
                }  
                
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictResponse objectForKey:@"msg"]);
                    return NO;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;
}

+ (BOOL)uploadPhotoWithUserId:(NSString*)userId avatarData:(NSData*)avatarData error:(NSError**)error
{   
    
    //[[JGoUtility sharedLock] lock];    
    
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/photoupload",JGO_HOST];
    NSURL *url = [NSURL URLWithString:urlString];
    NSString *BOUNDRY = [[NSProcessInfo processInfo] globallyUniqueString];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    
    LOG(@"url : %@ userId : %@ photoSize:%d", urlString,userId,[avatarData length]);
    
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDRY] forHTTPHeaderField:@"Content-Type"];
    
    NSMutableData *body = [NSMutableData dataWithCapacity:[avatarData length] + 512];
    
    // userId
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[userId dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    
    // file
    
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: attachment; name=\"photo\"; filename=\"alumnee.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:avatarData]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    
    // close form
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
    
    // set request body
    [theRequest setHTTPBody:body];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        
        //[[JGoUtility sharedLock] unlock];        
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/uploadPhoto.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictResponse = [responseString JSONValue];
            NSLog(@"%@",dictResponse);
            if (dictResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                //[[JGoUtility sharedLock] unlock];
                return NO;
            }
            else {
                NSString *status = [dictResponse objectForKey:@"return_code"];
                if (![status intValue]) {
                    //[[JGoUtility sharedLock] unlock];
                    return YES;
                }  
                
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictResponse objectForKey:@"msg"]);
                    //[[JGoUtility sharedLock] unlock];
                    return NO;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    //[[JGoUtility sharedLock] unlock];
    return NO;
    
}

+ (BOOL)saveSettingsWithUserId:(NSString*)userId statusPublic:(BOOL)statusPublic 
                  coursePublic:(BOOL)coursePublic modulesPublic:(BOOL)modulesPublic 
                  u_yearPublic:(BOOL)u_yearPublic collegePublic:(BOOL)collegePublic 
                         error:(NSError**)error
{
    NSString *keyval = [NSString stringWithFormat:@"status:%d;course:%d;modules:%d;u_year:%d;college:%d",
                        statusPublic,coursePublic,modulesPublic,u_yearPublic,collegePublic];
    
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/saveSettings?uid=%@&keyval=%@",JGO_HOST,userId,keyval];   
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/saveSettings.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];           
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    
                    if ([[dictionaryResponse objectForKey:@"returnCode"] integerValue] == 0) {                    
                        return YES;
                    }
                    else {
                        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                             [dictionaryResponse  objectForKey:@"msg"]);
                        return NO;
                    }
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return NO;
                }      
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;                      
}

+ (BOOL)keepAliveForUserId:(NSString*)userId error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/ping?uid=%@",JGO_HOST,userId];   
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    /*
    LOG(@"%@",urlString);
    */
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/ping.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];           
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    
                    if ([[dictionaryResponse objectForKey:@"returnCode"] integerValue] == 0) {                        
                        return YES;
                    }
                    else {
                        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                             [dictionaryResponse  objectForKey:@"msg"]);
                        return NO;
                    }
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return NO;
                }      
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;          
}

+ (NSDictionary*)getOnlineWithUserIds:(NSString*)userIds error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/getUserOnlineStatus?uids=%@",JGO_HOST,userIds];   
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getOnline.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];           
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    
                    if ([[dictionaryResponse objectForKey:@"returnCode"] integerValue] == 0) {
                        return dictionaryResponse;
                    }
                    else {
                        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                             [dictionaryResponse  objectForKey:@"msg"]);
                        return nil;
                    }
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return nil;
                }      
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;                   
}

+ (BOOL)createNewEventsWithUserId:(NSString*)userId title:(NSString*)title type:(NSString*)type cirType:(NSString*)cirType cirValue:(NSString*)cirValue scope:(NSString*)scope eventDate:(NSString*)eventDate startTime:(NSString*)startTime endTime:(NSString*)endTime address:(NSString*)address description:(NSString*)description lat:(NSString*)lat lon:(NSString*)lon error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/createNewEvent",JGO_HOST];
    LOG(@"urlString = %@", urlString);
    NSURL *url = [NSURL URLWithString:urlString];
    
     NSString *bodyString = [NSString stringWithFormat:@"uid=%@&title=%@&type=%@&cirType=%@&cirValue=%@&scope=%@&eventDate=%@&startTime=%@&endTime=%@&address=%@&description=%@&lat=%@&lon=%@",userId,title,type,cirType,cirValue,scope,eventDate,startTime,endTime,address,description,lat,lon]; 
    LOG(@"%@", bodyString);
    
    NSData *postBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    [theRequest setHTTPMethod:@"POST"];    
    [theRequest addValue:[NSString stringWithFormat:@"%d",[postBody length]] forHTTPHeaderField:@"Content-Length"];     
    [theRequest setHTTPBody:postBody];     
    [theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/createnewevent.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        NSLog(@"ResponseString= %@",responseString);
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictResponse = [responseString JSONValue];
            NSLog(@"**** dictResponse = %@  *****",dictResponse);
            if (dictResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                if ([[dictResponse objectForKey:@"returnCode"] integerValue] == 0) {
                    return YES;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictResponse objectForKey:@"msg"]);
                    return NO;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;
               
}

+ (NSArray*)getConversationsWithUserId:(NSString*)userId onlyOwner:(BOOL)onlyOwner error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/getConversations?uid=%@&only_owner=%@",
                           JGO_HOST,userId,onlyOwner ? @"true":@"false"];   
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getConversations.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];            
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    return [dictionaryResponse objectForKey:@"data"];
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return nil;
                }      
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;                       
}

+ (NSArray*)getMessagesWithUserId:(NSString*)userId conversationId:(NSString*)conversationId fromDate:(NSString*)fromDate toDate:(NSString*)toDate error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/getMessages?uid=%@&cid=%@&from=%@&to=%@",
                           JGO_HOST,userId,conversationId,fromDate,toDate];   
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    
    LOG(@"%@",urlString);
    
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getMessages.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];            
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    id items = [dictionaryResponse objectForKey:@"data"];
                    return [items isKindOfClass:[NSArray class]] ? items : nil;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return nil;
                }      
            }                    
        }        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;                     
}

+ (BOOL)addMessageWithUserId:(NSString*)userId conversationId:(NSString*)conversationId 
                        type:(NSString*)type content:(NSString*)content error:(NSError**)error
{
    
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/addMessage",JGO_HOST];
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSString *bodyString = [NSString stringWithFormat:@"uid=%@&cid=%@&type=%@&content=%@",userId,conversationId,type,content];
    
    LOG(@"%@", bodyString);
    
    NSData *postBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
    
    [theRequest setHTTPMethod:@"POST"];    
    [theRequest addValue:[NSString stringWithFormat:@"%d",[postBody length]] forHTTPHeaderField:@"Content-Length"];     
    [theRequest setHTTPBody:postBody];     
    [theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/addMessage.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes] 
                                                             length:[responseData length] 
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictResponse = [responseString JSONValue];
            
            if (dictResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                if ([[dictResponse objectForKey:@"returnCode"] isEqualToString:@"0"]) {
                    return YES;
                }  
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         [dictResponse objectForKey:@"msg"]);
                    return NO;
                }                                
            }            
        }        
    }
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;                
}

+ (NSDictionary*)createConversationWithUserId:(NSString*)userId conversationName:(NSString*)conversationName inviteeIds:(NSString*)inviteeIds error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/createConversation?name=%@&from_uid=%@&to_uids=%@",
                           JGO_HOST,conversationName,userId,inviteeIds];
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/createConversation.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes]
                                                             length:[responseData length]
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            NSLog(@"%@",dictionaryResponse);
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    
                    if ([[dictionaryResponse objectForKey:@"returnCode"] isEqualToString:@"0"]) {
                        return dictionaryResponse;
                    }
                    else {
                        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                             [dictionaryResponse objectForKey:@"msg"]);
                        return nil;
                    }
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return nil;
                }
            }
        }
        
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;
}

+ (NSDictionary*)getOfferDetailWithUserId:(NSString*)userId offerId:(NSString*)offerId error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/getOffer?uid=%@&id=%@",JGO_HOST,userId,offerId];
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getOfferDetail" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes]
                                                             length:[responseData length]
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    return [dictionaryResponse objectForKey:@"data"];
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return nil;
                }
            }
        }
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;
}

+ (NSDictionary*)getEventDetailWithUserId:(NSString*)userId eventId:(NSString*)eventId error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/getEvent?uid=%@&id=%@",JGO_HOST,userId,eventId];
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/getEventDetail" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes]
                                                             length:[responseData length]
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    return [dictionaryResponse objectForKey:@"data"];
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return nil;
                }
            }
        }
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;
}

+ (NSDictionary*)countMessagesWithUserId:(NSString*)userId activeConversationId:(NSString*)acid fromDate:(NSString*)fromDate error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/countMessages?uid=%@&xcid=%@&from=%@",JGO_HOST,userId,acid,fromDate];
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return nil;
    }
    else {
        [responseData writeToFile:@"/JGO/countMessages.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes]
                                                             length:[responseData length]
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return nil;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    return dictionaryResponse;
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return nil;
                }
            }
        }
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return nil;
}

+ (BOOL)deleteConversationsWithUserId:(NSString*)userId conversationIds:(NSString*)cids error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/deleteConversation?uid=%@&cids=%@",JGO_HOST,userId,cids];
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/deleteConversations" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes]
                                                             length:[responseData length]
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    
                    if ([[dictionaryResponse objectForKey:@"returnCode"] isEqualToString:@"0"]) {
                        return YES;
                    }
                    else {
                        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                             [dictionaryResponse objectForKey:@"msg"]);
                        return NO;
                    }
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return NO;
                }
            }
        }
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;
}


+ (BOOL)deleteMessagesWithMessageIds:(NSString*)mids error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/deleteMessages?mids=%@",JGO_HOST,mids];
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/deleteMessages.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes]
                                                             length:[responseData length]
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    
                    if ([[dictionaryResponse objectForKey:@"returnCode"] isEqualToString:@"0"]) {
                        return YES;
                    }
                    else {
                        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                             [dictionaryResponse objectForKey:@"msg"]);
                        return NO;
                    }
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return NO;
                }
            }
        }
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;    
}

+ (BOOL)updateEventStatusWithUserId:(NSString*)userId eventId:(NSString*)eventId status:(NSString*)status error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/userEventStatusUpdate?uid=%@&event_id=%@&status=%@",JGO_HOST,userId,eventId,status];
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/userEventStatusUpdate.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes]
                                                             length:[responseData length]
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    
                    if ([[dictionaryResponse objectForKey:@"returnCode"] integerValue] == 0) {
                        return YES;
                    }
                    else {
                        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                             [dictionaryResponse objectForKey:@"msg"]);
                        return NO;
                    }
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return NO;
                }
            }
        }
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;    
}

+ (BOOL)deleteEventsWithUserId:(NSString*)userId eventIds:(NSString*)eventIds error:(NSError**)error
{
    NSString *urlString = [NSString stringWithFormat:@"%@/index.php/api/multiEventsDelete?uid=%@&listEvent=%@",JGO_HOST,userId,eventIds];
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    LOG(@"%@",urlString);
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    
    NSURLResponse *response = nil;
    NSError *aError = nil;
    NSData *responseData = nil;
    
    responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&aError];
    
    if (aError != nil || response == nil) {
        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_NETWORK,
                             JGO_ERROR_MSG_NETWORK);
        return NO;
    }
    else {
        [responseData writeToFile:@"/JGO/multiEventsDelete.txt" atomically:YES];
        NSString *responseString = [[[NSString alloc] initWithBytes:[responseData bytes]
                                                             length:[responseData length]
                                                           encoding:NSUTF8StringEncoding] autorelease];
        
        if ([responseString isKindOfClass:[NSString class]]) {
            NSDictionary *dictionaryResponse = [responseString JSONValue];
            
            if (dictionaryResponse == nil) {
                JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_DATA,
                                     JGO_ERROR_MSG_DATA);
                return NO;
            }
            else {
                if ([dictionaryResponse isKindOfClass:[NSDictionary class]]) {
                    
                    if ([[dictionaryResponse objectForKey:@"returnCode"] integerValue] == 0) {
                        return YES;
                    }
                    else {
                        JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                             [dictionaryResponse objectForKey:@"msg"]);
                        return NO;
                    }
                }
                else {
                    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_FROM_SERVER,
                                         JGO_ERROR_MSG_DATA);
                    return NO;
                }
            }
        }
    }
    
    JGO_API_HANDLE_ERROR(JGO_ERROR_CODE_UNKNOW,
                         JGO_ERROR_MSG_UNKNOW);
    return NO;    
}

@end
